﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RoadTeam_Generate
{
    public class SqlEmun
    {
        #region existssysproperties
           public const String existssysproperties = @"if exists (select 1 from sysobjects where name = 'sysproperties'and xtype = 'V')
             DROP VIEW sysproperties";
        #endregion
        #region Createsysproperties
        public const String Createsysproperties =@"
    CREATE VIEW sysproperties
    AS
    SELECT A.name As TableName,
    A.id As TableID,B.Name As ColName,B.colid As ColID,
    B.xtype As ColType,C.name As PropName,C.Value As PropValue
    FROM sysobjects As A INNER JOIN syscolumns As B ON A.id = B.id
    INNER JOIN sys.extended_properties As C ON C.major_id = A.id 
    AND ( minor_id = B.colid)";
        #endregion
        #region existDBSYSINFO
       public const String existDBSYSINFO = @"if exists (select 1 from sysobjects where name = 'DBSYSINFO'and xtype = 'V')
drop view DBSYSINFO";
       #endregion
        #region CreateDBSYSINFO
       

       public const String CreateDBSYSINFO = @"Create view DBSYSINFO
AS
SELECT 
  --表名   = CASE a.colorder WHEN 1 THEN c.name ELSE '' END, 
  表名   =c.name,
  序     = a.colorder, 
  字段名 = a.name, 
  标识   = CASE COLUMNPROPERTY(a.id,a.name,'IsIdentity') WHEN 1 THEN '是' ELSE '' END, 
  主键   = CASE 
    WHEN EXISTS ( 
      SELECT * 
      FROM sysobjects 
      WHERE xtype='PK' AND name IN ( 
        SELECT name 
        FROM sysindexes 
        WHERE id=a.id AND indid IN ( 
          SELECT indid 
          FROM sysindexkeys 
          WHERE id=a.id AND colid IN ( 
            SELECT colid 
            FROM syscolumns 
            WHERE id=a.id AND name=a.name 
          ) 
        ) 
      ) 
    ) 
    THEN '是' 
    ELSE '' 
  END, 
  类型   = b.name, 
  字节数 = a.length, 
  长度   = COLUMNPROPERTY(a.id,a.name,'Precision'), 
  小数   = CASE ISNULL(COLUMNPROPERTY(a.id,a.name,'Scale'),0) 
    WHEN 0 THEN '' 
    ELSE CAST(COLUMNPROPERTY(a.id,a.name,'Scale') AS VARCHAR) 
  END, 
  允许空 = CASE a.isnullable WHEN 1 THEN '是' ELSE '' END, 
  默认值 = ISNULL(d.[text],'')
, 说明   = ISNULL(e.[PropValue],'') 
FROM syscolumns a 
  LEFT  JOIN systypes      b ON a.xtype=b.xusertype 
  INNER JOIN sysobjects    c ON a.id=c.id AND c.xtype='U' AND c.name<>'dtproperties' 
  LEFT  JOIN syscomments   d ON a.cdefault=d.id 
  LEFT  JOIN sysproperties e ON a.id=e.TableID AND a.colid=e.ColID ";

       #endregion

    }
}
